<?php
namespace Model;

use Think\Model;

class SkillModel extends CommonModel
{

    //每页的数量
    public  $each_page_number = 3;
      
    protected $_validate = array(
		//①验证手机号码是否为纯数字
        array('user_account', 'number', '{"msg":"手机号格式错误"}'),
		
		//②验证手机号码是否为11位
        array('user_account', '11', '{"msg":"请输入正确的手机号码"}', 0, 'length'),
		
		//③验证手机号码是否已被注册
        array('user_account', '', '{"msg":"该手机已被注册"}', 0, 'unique'),
		
		//验证微信号
        array('user_account', '', '{"msg":"该手机已被注册"}', 0, 'unique'),

    );


    //获取技能共享中心的所有技能，以时间降序排列（已审核通过）
    public function model_get_all_skill()
    {
        $skill = D('skill');
        $sql = "SELECT 
            user_id,user_nickname,user_sex,skill_id,skill_price,skill_unit,skill_title,skill_issue_time
            FROM wx_skill,wx_user
            WHERE wx_skill.skill_user = wx_user.user_id
            AND skill_state = 1
            order by skill_issue_time DESC";

        $info = $skill->query($sql);
        return $info;
    }


    //获取用户发布的技能，以时间降序排列
    /**
     * $user_id 用户id
     * $skill_state 技能的状态
     * $page 页数$page
     */
    public function model_get_user_skill_by_page($page, $user_id, $skill_state)
    {
        //根据页码计算查询的起始值
        $page_start = ($page - 1) * $this->each_page_number;   

        $skill = D('skill');
        $sql = "SELECT user_id,user_nickname,user_sex, skill_id,skill_price,skill_unit,skill_title,skill_issue_time
        FROM wx_skill,wx_user
        WHERE wx_skill.skill_user = wx_user.user_id
        AND skill_state = " . $skill_state . "
        AND wx_skill.skill_user = " . $user_id . "
        ORDER BY skill_issue_time 
        DESC
        LIMIT " . $page_start . "," . $this->each_page_number;

        $info = $skill->query($sql);
        return $info;
    }


    //根据页码获取技能，以时间降序排列
    /**
     * $page 页码
     * $skill_state 技能的状态
     */
    public function model_get_skill_by_page($page,$skill_state)
    {
        //根据页码计算查询的起始值
        $page_start = ($page - 1) * $this->each_page_number;    

        $skill = D('skill');
        $sql = "SELECT user_id, user_nickname,user_sex,skill_id,skill_price,skill_unit,skill_title,skill_issue_time
            FROM wx_skill,wx_user
            WHERE wx_skill.skill_user = wx_user.user_id
            AND skill_state = ".$skill_state."
            order by skill_issue_time 
            DESC
            limit " . $page_start . "," . $this->each_page_number;

        $info = $skill->query($sql);
        return $info;
    }


        
    //查看技能详细信息
    public function model_get_skill_detail($skill_id)
    {
        $skill = D('skill');
        $sql = 'SELECT user_id,user_nickname,user_sex, user_tel,user_qq, user_wechat,user_default_contact_way, skill_id,skill_price,
            skill_unit,skill_title,skill_start_location,skill_end_location,skill_start_time,skill_end_time,skill_issue_time,skill_describe
            FROM wx_skill,wx_user
            WHERE wx_skill.skill_user = wx_user.user_id 
            AND skill_id=' . $skill_id . '
            order by skill_issue_time DESC';

        $info_arr = $skill->query($sql);
        $info = $info_arr[0];
        return $info;
    }


    //商品下架
    public function model_skill_outofstock($skill_id) {
        $skill = D('skill');
        $data = array(
			'skill_state' => -2,
		);
		$info = $skill->where('skill_id=' . $skill_id)->setField($data);
		return $info;
    }


    //设置商品的状态为2（商品被预约）
    public function model_set_skillstate_to_2($skill_id) {
        $skill = D('skill');
        $data = array(
			'skill_state' => 2,
		);
		$info = $skill->where('skill_id=' . $skill_id)->setField($data);
		return $info;
    }

}